<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <link rel="stylesheet" href="https://gxwtf.cn/css/fa-pro/css/all.min.css">
    <link rel="stylesheet" href="/css/basic.css">
    <style>
        .poem-container {
            margin: 20px auto;
            background: #fff;
            padding: 40px;
        }

        .poem-header {
            margin-bottom: 20px;
        }

        .poem-title {
            font-size: 36px;
            color: var(--primary-color);
            margin-bottom: 20px;
            text-align: center;
        }

        .poem-meta {
            color: #6c757d;
            font-size: 24px;
            margin-bottom: 30px;
            text-align: center;
        }

        .poem-tags {
            display: flex;
            justify-content: center;
            gap: 8px;
            margin: 12px 0;
            flex-wrap: wrap;
        }

        .poem-tag {
            background: rgba(163, 31, 36, 0.1);
            color: var(--primary-color);
            padding: 4px 12px;
            border-radius: 14px;
            font-size: 14px;
            border: 1px solid rgba(163, 31, 36, 0.3);
            transition: all 0.3s;
            cursor: pointer;
        }

        .poem-tag:hover {
            background: var(--primary-color);
            color: white;
            transform: translateY(-1px);
        }

        .text-center {
            text-align: center !important;
            text-indent: 0 !important;
        }

        .text-left {
            text-indent: 2em;
        }

        .line-container {
            margin: 12px 0;
            font-size: 24px;
        }

        .toggle {
            background: var(--primary-color);
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            margin-bottom: 20px;
            transition: background 0.3s;
        }

        .toggle:hover {
            background: var(--primary-color-hover);
        }

        .poem-content {
            line-height: 1.8;
        }

        .original-line {
            margin: 12px 0;
            color: #212529;
        }

        .translated-line {
            color: #6c757d;
            font-size: 0.9em;
            display: none;
        }

        .show-translation .translated-line {
            display: block;
        }

        .controls-container {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin-bottom: 20px;
        }

        /* 拼音显示样式 */
        .pinyin-line {
            display: none;
            color: #8a1a1f;
            font-size: 0.8em;
            text-align: center;
            line-height: 1.6;
            letter-spacing: 1.7px;
            margin-bottom: 4px;
            opacity: 0.85;
        }

        .show-pinyin .pinyin-line {
            display: block;
        }

        .section-title {
            font-size: 20px;
            color: var(--primary-color);
            border-bottom: 2px solid var(--primary-color);
            padding-bottom: 8px;
            margin-bottom: 20px;
            display: inline-block;
        }

        .paragraph {
            font-size: 1.08em;
            color: #444;
            line-height: 2;
            margin-bottom: 0;
            margin-top: 0;
            padding: 0;
            text-align: left;
            word-break: break-all;
            text-indent: 2em;
        }

        .poem-card {
            padding: 8px 18px;
            border-radius: 6px;
            background: #fff;
            border: 1px solid #eee;
            transition: all 0.3s ease;
            cursor: pointer;
            text-decoration: none;
            color: var(--primary-color);
            font-size: 15px;
            margin-right: 10px;
            margin-bottom: 10px;
            display: inline-block;
        }

        .poem-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            border-color: var(--primary-color);
            color: var(--primary-color);
            background: #f8f9fa;
        }

        .poem-tags-area {
            margin: 30px 0 0 0;
            text-align: left;
        }

        .poem-tags-list {
            margin-bottom: 10px;
        }

        @media (max-width: 768px) {
            body {
                padding: 20px 10px;
            }

            .poem-container {
                padding: 20px;
            }

            .line-container {
                font-size: 18px;
            }

            .poem-title {
                font-size: 28px;
            }

            .poem-meta {
                font-size: 18px;
            }

            .controls-container {
                flex-direction: column;
                align-items: center;
                gap: 10px;
            }

            .pinyin-line {
                letter-spacing: 1.3px;
            }
        }
    </style>
    <script src="/js/navbar.js"></script>
    <script src="/js/backButton.js"></script>
    <script src="/js/footer.js"></script>
</head>

<body>
    <div id="navbar-container"></div>

    <div class="poem-container">
        <a href="#" class="back-button">
            <i class="fa-solid fa-arrow-left"></i>
            返回
        </a>

        <div class="poem-header">
            <h1 class="poem-title" id="poemTitle"></h1>
            <div class="poem-meta" id="poemMeta"></div>
        </div>

        <div class="controls-container">
            <button class="toggle" onclick="toggleTranslation()">
                <i class="fa-solid fa-language"></i>
                <span id="translationText">显示翻译</span>
            </button>
            <button class="toggle" onclick="togglePinyin()">
                <i class="fa-solid fa-icons"></i>
                <span id="pinyinText">显示拼音</span>
            </button>
        </div>

        <div class="poem-content" id="poemContent"></div>
        <div class="poem-tags-area">
            <div class="poem-tags-list" id="poemTags"></div>
        </div>
        <div style="margin-top:30px;">
            <div class="section-title" style="margin-bottom:12px;">写作背景</div>
            <div id="poemBackground" class="paragraph"></div>
        </div>
        <div style="margin-top:30px;">
            <div class="section-title" style="margin-bottom:12px;">内容赏析</div>
            <div id="poemAnalysis" class="paragraph"></div>
        </div>
    </div>

    <div id="footer-container"></div>

    <script>
        async function loadPoem() {
            const title = decodeURIComponent(location.pathname.slice(1));
            try {
                const response = await fetch(`/poem?title=${title}`);
                if (!response.ok) throw new Error('未找到对应的古诗文');
                let poem = await response.json();
                poem = poem[0];

                document.title = poem.title;
                document.getElementById('poemTitle').textContent = poem.title;

                // 添加标签展示（用poem-card样式）
                const tagsHTML = poem.tags?.map(tag =>
                    `<span class="poem-card">${tag}</span>`
                ).join('') || '';
                document.getElementById('poemTags').innerHTML = tagsHTML;

                // 设置作者链接
                document.getElementById('poemMeta').innerHTML = `
                    【${poem.dynasty}】<a href="/author/${encodeURIComponent(poem.author)}" 
                     style="color: var(--primary-color-hover);text-decoration: none;">
                     ${poem.author}</a>
                `;

                // 生成内容，分别生成带拼音和不带拼音的 html
                const contentHTML = poem.content.map(c => {
                    const displayClass = c.display || poem.display ? 'text-' + (c.display || poem.display) : '';
                    const noPinyinHTML = generateNoPinyinHTML(c.line);
                    const withPinyinHTML = generateRubyHTML(c.line, c.pinyin);
                    return `
                        <div class="line-container ${displayClass}">
                            <div class="original-line ${displayClass}">
                                <span class="no-pinyin">${noPinyinHTML}</span>
                                <span class="with-pinyin" style="display:none;">${withPinyinHTML}</span>
                            </div>
                            <div class="translated-line">${c.trans}</div>
                        </div>
                    `;
                }).join('');

                document.getElementById('poemContent').innerHTML = contentHTML;

                // 写作背景（如有），支持多段落，\n分割
                const bg = poem.background || '暂无写作背景信息。';
                const bgParagraphs = bg.split('\n').map(p => `<p class="paragraph">${p}</p>`).join('');
                document.getElementById('poemBackground').innerHTML = bgParagraphs;

                // 内容赏析（如有），支持多段落，\n分割
                const analysis = poem.analysis || '暂无内容赏析。';
                const analysisParagraphs = analysis.split('\n').map(p => `<p class="paragraph">${p}</p>`).join('');
                document.getElementById('poemAnalysis').innerHTML = analysisParagraphs;
            } catch (error) {
                document.body.innerHTML = '<h1>加载失败</h1>';
                console.error(error);
            }
        }

        // 只输出原文，不加拼音
        function generateNoPinyinHTML(line) {
            return line;
        }

        // 拼音用ruby，标点不加拼音，拼音字符串以空格分隔
        function generateRubyHTML(line, pinyinStr) {
            if (!pinyinStr) return line;
            const pinyinArr = pinyinStr.split(' ');
            let html = '';
            let pinyinIdx = 0;
            for (let i = 0; i < line.length; i++) {
                const char = line[i];
                if (/[\u4e00-\u9fa5]/.test(char)) {
                    const py = pinyinArr[pinyinIdx] || '';
                    html += `<ruby>${char}<rt>${py}</rt></ruby>`;
                    pinyinIdx++;
                } else {
                    html += char;
                }
            }
            return html;
        }

        function toggleTranslation() {
            const container = document.getElementById('poemContent');
            const button = document.querySelector('.toggle[onclick="toggleTranslation()"]');
            container.classList.toggle('show-translation');
            button.querySelector('span').textContent = container.classList.contains('show-translation')
                ? '隐藏翻译'
                : '显示翻译';
        }

        function togglePinyin() {
            const container = document.getElementById('poemContent');
            const button = document.querySelector('.toggle[onclick="togglePinyin()"]');
            const isShow = container.classList.toggle('show-pinyin');
            button.querySelector('span').textContent = isShow
                ? '隐藏拼音'
                : '显示拼音';

            // 切换所有行的拼音显示
            document.querySelectorAll('.original-line').forEach(line => {
                const noPinyin = line.querySelector('.no-pinyin');
                const withPinyin = line.querySelector('.with-pinyin');
                if (isShow) {
                    noPinyin.style.display = 'none';
                    withPinyin.style.display = '';
                } else {
                    noPinyin.style.display = '';
                    withPinyin.style.display = 'none';
                }
            });
        }

        // 默认隐藏拼音
        document.addEventListener('DOMContentLoaded', () => {
            document.getElementById('poemContent').classList.remove('show-pinyin');
            const pinyinBtn = document.querySelector('.toggle[onclick="togglePinyin()"]');
            if (pinyinBtn) pinyinBtn.querySelector('span').textContent = '显示拼音';
        });

        loadPoem();
    </script>
</body>

</html>